package com.app.reading;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hwangjr.rxbus.RxBus;
import com.kunfei.basemvplib.BitIntentDataManager;
import com.kunfei.bookshelf.DbHelper;
import com.kunfei.bookshelf.base.observer.MyObserver;
import com.kunfei.bookshelf.bean.BookChapterBean;
import com.kunfei.bookshelf.bean.BookInfoBean;
import com.kunfei.bookshelf.bean.BookShelfBean;
import com.kunfei.bookshelf.bean.LocBookShelfBean;
import com.kunfei.bookshelf.constant.RxBusTag;
import com.kunfei.bookshelf.help.BookshelfHelp;
import com.kunfei.bookshelf.model.ImportBookModel;
import com.kunfei.bookshelf.model.WebBookModel;
import com.kunfei.bookshelf.presenter.BookDetailPresenter;
import com.kunfei.bookshelf.presenter.ReadBookPresenter;
import com.kunfei.bookshelf.utils.RxUtils;
import com.kunfei.bookshelf.view.activity.BookDetailActivity;
import com.kunfei.bookshelf.view.activity.ReadBookActivity;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.kunfei.bookshelf.utils.StringUtils.getString;

public class MainActivity extends AppCompatActivity {
    TextView btn_no;
    Button btn_yes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        ActionBar actionbar =getSupportActionBar();
//        if(actionbar!=null){
//            actionbar.hide();
//        }
        initView();
    }

    public static List<Activity> activityList=new LinkedList<>();

    private void initView() {
        btn_no=this.findViewById(R.id.btn_no);
        btn_yes=this.findViewById(R.id.btn_yes);
        btn_yes.setOnClickListener(v -> {
            //新增
            File file = new File("/storage/emulated/0/Download/晨阳.txt");
            BookShelfBean bookShelfBean = BookshelfHelp.getBook(file.getAbsolutePath());
            if(bookShelfBean == null) {
                ImportBookModel.getInstance().importBook(file).subscribe(new Observer<LocBookShelfBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull LocBookShelfBean value) {
                        if (value.getNew()) {

                            Intent intent = new Intent(MainActivity.this, ReadBookActivity.class);
                            intent.putExtra("openFrom", ReadBookPresenter.OPEN_FROM_APP);
                            String key = String.valueOf(System.currentTimeMillis());
                            String bookKey = "book" + key;
                            intent.putExtra("bookKey", bookKey);
                            BitIntentDataManager.getInstance().putData(bookKey, value.getBookShelfBean().clone());
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

            }else{
                Intent intent = new Intent(MainActivity.this, ReadBookActivity.class);
                intent.putExtra("openFrom", ReadBookPresenter.OPEN_FROM_APP);
                String key = String.valueOf(System.currentTimeMillis());
                String bookKey = "book" + key;
                intent.putExtra("bookKey", bookKey);
                BitIntentDataManager.getInstance().putData(bookKey, bookShelfBean.clone());
                startActivity(intent);
            }
        });
        btn_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
        MainActivity.activityList.add(this);
    }

    private Observable<BookShelfBean> saveBookToShelfO(BookShelfBean bookShelfBean, List<BookChapterBean> chapterBeanList) {
        return Observable.create(e -> {
            if (!chapterBeanList.isEmpty()) {
                BookshelfHelp.delChapterList(bookShelfBean.getNoteUrl());
                BookshelfHelp.saveBookToShelf(bookShelfBean);
                DbHelper.getDaoSession().getBookChapterBeanDao().insertOrReplaceInTx(chapterBeanList);
            }
            e.onNext(bookShelfBean);
            e.onComplete();
        });
    }

    public void exit(){
        for(Activity act:activityList){
            act.finish();
        }
        System.exit(0);
    }
}
